define(function(require, exports, module) {
    require('{component}popup/popup.css');
    var mId = 'found',
        scrollStop = false,
        recommendedData,
        popup = require('{component}popup/popup'),
        container = require("{component}container/container"),
        tplContainer = require("{view}container.tpl"),
        tpl = require("{view}found.tpl"),
        tplSearch = require("{view}search.tpl"),
        tplType = require("{view}container_type.tpl"),
        tplRecommendedList = require("{view}recommendedList.tpl"),
        popupImg = require("{component}popupImg/popupImg"),
        tplPopupImg = require("{view}popupImg.tpl"),
        imglazyload = require("{component}imglazyload/imglazyload"),

        view = {

            template: _.template(tpl),
            templateContainer: _.template(tplContainer),
            templateType: _.template(tplType),
            templateSearch: _.template(tplSearch),
            templateRecommendedList: _.template(tplRecommendedList),
            // tplSearch
            render: function() {
                var parameter = share.getParameter(),
                    type = parameter.type;

                if ($('#' + mId).length < 1) {
                    // templateContainer
                    $("body").append(this.template());
                    share.container(mId, container, this.templateContainer);
                    this.bindEvt();
                    this.ajaxTops();
                } else {
                    share.showPage(mId);
                    share.tabSwitch($('#' + mId), 0);
                }
                this.reload();
            },
            reload: function() {
                var dom = $('#' + mId);
                dom.find('.body').scrollTop(1);
                container.reload(mId);
            },
            renderList: function(data) {
                var dom = $('#' + mId),
                    typeData = data || 　util.getLocalStorage('type');
                share.cLoading('hide', mId);
                console.log(typeData)
                dom.find('.tab_tops').html(this.templateType(typeData));
                dom.find('.tabTitle li').eq(0).attr('ajax', 1);
                this.sortingList();
                share.tabSwitch(dom, 0);
                this.reload();
                dom.imglazyload();
            },
            ajaxRecommended: function(songid) {
                var tthis = this,
                    dom = $('#' + mId);
                $.ajax(share.ajax({
                    url: apiUrl,
                    data: { method: 'baidu.ting.song.getRecommandSongList', song_id: songid, num: 50 },
                    success: function(data) {
                        recommendedData = data;
                        dom.find('.tab_recommended').html(tthis.templateRecommendedList(data));
                        dom.find('.tabTitle li').eq(1).attr('ajax', 1);
                        share.cLoading('hide');
                        dom.imglazyload();
                    }
                }))
            },
            bindEvt: function() {
                var tthis = this,
                    dom = $("#" + mId);
                // share.tabSwitch(dom,0);

                dom.find('.tabTitle').find('li').on('tap', function() {
                    var index = $(this).index();
                    dom.find('.body').scrollTop(1);
                    if ($(this).attr('ajax')) {
                        share.cLoading('hide');
                    } else {
                        share.cLoading();
                        if (index == 1) {
                            tthis.ajaxRecommended(267767595);
                        }
                    }
                    share.tabSwitch(dom, index);
                })

                dom.find('.body').on('scroll', function() {
                    if (scrollStop) return;
                    var curTop;
                    // console.log($(this).scrollTop())
                    if ($(this).scrollTop() <= 0) {
                        curTop = $(this).scrollTop();
                        $(this).scrollTop(1);
                    }
                    if (curTop < -80) {
                        $(this).find('h4').css('margin-top', '-22px')

                        scrollStop = true;
                        share.cLoading();
                        tthis.ajaxTops(true);
                    }
                });

                // 调试代码 注意这里h4
                // $('body').on('tap','#'+mId+' h4',function(){
                // 	$(this).css('margin-top','-30px')
                // 	scrollStop = true;
                // 	share.cLoading();
                // 	tthis.ajaxTops(true);
                // });

                dom.find('.tab_tops').on('tap', function(e) {
                    var target = $(e.target),
                        curLi = target.closest('li'),
                        curDiv = target.closest('div');
                    curlabel = target.closest('label');
                    if (curDiv.hasClass('line')) {
                        var type = curDiv.attr('type');
                        window.location.href = '#typeList?type=' + type;
                    }
                });

                dom.find('.tab_recommended').on('tap', function(e) {
                    var target = $(e.target),
                        curLi = target.closest('li'),
                        curI = target.closest('i'),
                        curImg = target.closest('img');
                    if (curImg.attr('src')) {
                        curImg.popupImg();
                        return;
                    }
                    if (curI.hasClass('ico_more')) {
                        var songid = curI.parents('li').attr('songid'),
                            tinguid = curI.parents('li').attr('tinguid');
                        popup.show({ type: 'more', songid: songid, tinguid: tinguid, li: ['recommended', 'singer'] });
                        return;
                    }
                    if (curLi.attr('songid')) {
                        var songid = curLi.attr('songid');
                        util.setLocalStorage('play_list', recommendedData.result);
                        window.location.href = '#play?songid=' + songid;
                        return;
                    }
                });
            },

            sortingList: function() {
                var tthis = this,
                    dom = $('#' + mId),
                    musicType = share.musicTypeArr(),
                    i = 0;
                var arr = [23, 20, 24, 11, 25, 22, 21, 2, 1];
                var domLine = dom.find('.line');
                $.each(arr, function(i) {
                    var line = dom.find('.line.type' + arr[i]);
                    dom.find('.line_box').prepend(line);
                })
            },
            ajaxTops: function(ajaxBool) {
                var tthis = this,
                    dom = $('#' + mId),
                    musicType = share.musicTypeArr(),
                    i = 0;
                var typeData = util.getLocalStorage('type');

                // typeData=false;
                if (typeData && (!ajaxBool)) {
                    this.renderList();
                } else {
                    var newObj = { data: {} };
                    _.each(musicType, function(obj) {
                        (function() {
                            var type = obj.type;
                            $.ajax(share.ajax({
                                url: apiUrl,
                                data: { method: 'baidu.ting.billboard.billList', type: obj.type, size: 50, offset: 0 },
                                success: function(data) {
                                    // console.log(type,data)
                                    if (i >= musicType.length - 1) {
                                        localStorage.removeItem('music_type');
                                        util.setLocalStorage('type', newObj);
                                        tthis.renderList(newObj);
                                        share.loading('hide');
                                        scrollStop = false;
                                    }
                                    if (data.song_list.length) {
                                        newObj.data['type' + obj.type] = data;
                                    }
                                    i++;
                                }
                            }));
                        })();
                    });
                }
            },
        };

    view.userNav = false;
    module.exports = view;
});
